﻿using _1._7._2015_EF_Movies.Data;
using _1._7._2015_EF_Movies.Data.Models;
using _1._7._2015_EF_Movies.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity.Migrations;

namespace _1._7._2015_EF_Movies.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult About()
        {

            return View();
        }

        public ActionResult Index()
        {

            MovieDbContext db = new MovieDbContext();
            MoviesViewModel model = new MoviesViewModel();

            model.Movies = db.Movies.Select(m => new MovieViewModel
            {
                MovieId = m.MovieId,
                Description = m.Description,
                PosterUrl = m.PosterUrl,
                Rating = m.Rating,
                Title = m.Title
            }).ToList();

            return View(model);
        }

        public ActionResult Detail(int id)
        {
            MovieDbContext db = new MovieDbContext();
            MovieViewModel model = new MovieViewModel();
            Movie movie = db.Movies.FirstOrDefault(m => m.MovieId == id);

            model.Description = movie.Description;
            model.MovieId = movie.MovieId;
            model.PosterUrl = movie.PosterUrl;
            model.Rating = movie.Rating;
            model.Title = movie.Title;

            return View(model);
        }

        public ActionResult Update(int id)
        {
            MovieDbContext db = new MovieDbContext();
            MovieViewModel model = new MovieViewModel();
            Movie movie = db.Movies.FirstOrDefault(m => m.MovieId == id);

            model.Description = movie.Description;
            model.MovieId = movie.MovieId;
            model.PosterUrl = movie.PosterUrl;
            model.Rating = movie.Rating;
            model.Title = movie.Title;

            return View(model);
        }

        [HttpPost]
        public ActionResult Update(MovieViewModel model, int id)
        {
            MovieDbContext db = new MovieDbContext();
            Movie movie = db.Movies.FirstOrDefault(m => m.MovieId == id);


            movie.Description = model.Description;
            movie.PosterUrl = model.PosterUrl;
            movie.Rating = model.Rating;
            movie.Title = model.Title;
  
            db.SaveChanges();

            return RedirectToAction("Detail", new { Id = id });
        }

        public ActionResult Delete(int id)
        {
            MovieDbContext db = new MovieDbContext();
            Movie movie = db.Movies.FirstOrDefault(m => m.MovieId == id);
            db.Movies.Remove(movie);

            db.SaveChanges();

            return RedirectToAction("Index");
        }

        public ActionResult Create()
        {
            MovieViewModel model = new MovieViewModel();

            return View(model);
        }

        [HttpPost]
        public ActionResult Create(MovieViewModel model)
        {
            MovieDbContext db = new MovieDbContext();

            db.Movies.AddOrUpdate(m => new { m.Title, m.Description, m.Rating, m.PosterUrl },
                new Movie
                {
                    Description = model.Description,
                    PosterUrl = model.PosterUrl,
                    Rating = model.Rating,
                    Title = model.Title
                });
            db.SaveChanges();
            return RedirectToAction("Index");
        }


        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}